import request from '../httpRequest'
import type { IResponse } from '../../types/api'

export interface LoginRequest {
  username?: string;
  phone?: string;
  password: string;
}

export interface LoginResponse {
  user: {
    id: string;
    username: string;
    phone: string;
    role: string;
    status: string;
    create_time: Date;
  };
  token: string;
  expires_in: number;
}

export interface RegisterRequest {
  username: string;
  password: string;
  phone: string;
}

export interface ChangePasswordRequest {
  oldPassword: string;
  newPassword: string;
}

export const authApi = {
  // 登录
  login(data: LoginRequest): Promise<IResponse<LoginResponse>> {
    return request.post('/auth/login', data)
  },

  // 注册
  register(data: RegisterRequest): Promise<IResponse<LoginResponse>> {
    return request.post('/auth/register', data)
  },

  // 登出
  logout(): Promise<IResponse<null>> {
    return request.post('/auth/logout')
  },

  // 修改密码
  changePassword(data: ChangePasswordRequest): Promise<IResponse<null>> {
    return request.post('/auth/change-password', data)
  },

  // 获取当前用户信息
  getCurrentUser(): Promise<IResponse<LoginResponse['user']>> {
    return request.get('/auth/me')
  }
}
